var express = require('express'),
  router = express.Router(),
  fs = require('fs'),
  basic = require('../../basicmodule/basic.js');

module.exports = function (app) {
  app.use('/', router);
};

var utils = require('../tool/Utils.js')
const DIRPATH = "freedom" + utils.format + "upload" + utils.format;


router.get('/', function (req, res, next) {
  refresh(req, res, next);
});

router.get(/^\/index$/, function (req, res, next) {
  // body...
  refresh(req, res, next)
});


refresh = function (req, res, next) {
  res.setHeader('Cache-Control', "max-age=" + 120);
  var expires = new Date();
  expires.setTime(expires.getTime() + 120 * 1000);
  res.setHeader('Expires', expires.toGMTString());


  let data = basic.getGenFiles(DIRPATH)
  let jsonFileInfo = JSON.stringify(data.file_info);
  let jsonGenPath = JSON.stringify(data.gen_path);

  res.render('pages/index', {
    file_info: jsonFileInfo,
    gen_path: jsonGenPath,
    is_root: false,
  });
};

router.get('/index.html', function (req, res, next) {
  // body...
  refresh(req, res, next)
});

router.post('/index.html', function (req, res) {
  // body...
  res.send(basic.getGenFiles(DIRPATH));
})
